package com.we.flink.dcapiuserlabel.tag.telsale;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.dcapiuserlabel.table.TableAiTask;

public class TagAiTask {
    public long uid;
    public int telsale_ai_reach_time;

    public TagAiTask(long uid, int telsale_ai_reach_time) {
        this.uid = uid;
        this.telsale_ai_reach_time = telsale_ai_reach_time;
    }

    public static TagAiTask init(long uid) {
        return new TagAiTask(uid, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableAiTask.QUERYSQL);
        DataStream<Tuple2<Boolean, TableAiTask>> inputStream =
                tableEnv.toRetractStream(tableRes, TableAiTask.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableAiTask>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableAiTask> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("TagAiTask-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableAiTask>, Long>() {

                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableAiTask> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<Tuple2<Boolean, TableAiTask>, String>() {
                                    ValueState<TagAiTask> aiTaskVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        aiTaskVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<TagAiTask>(
                                                                        "AiTaskVState",
                                                                        TagAiTask.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableAiTask> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int create_time =
                                                (int) (value.f1.getCreate_time().getTime() / 1000);

                                        TagAiTask tmpState = aiTaskVState.value();
                                        TagAiTask curState =
                                                tmpState == null ? TagAiTask.init(uid) : tmpState;

                                        if (create_time > curState.gettelsale_ai_reach_time()) {
                                            curState.telsale_ai_reach_time = create_time;
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("uid", uid);
                                            jsonObject.put("telsale_ai_reach_time", create_time);

                                            out.collect(jsonObject.toString());
                                            aiTaskVState.update(curState);
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }

    public int gettelsale_ai_reach_time() {
        return telsale_ai_reach_time;
    }
}
